package ai4games_tests.tests;

import game2dai.Domain;
import game2dai.World;
import game2dai.entities.Building;
import game2dai.entities.Obstacle;
import game2dai.entities.Vehicle;
import game2dai.entityshapes.ps.BuildingPic;
import game2dai.entityshapes.ps.Hints;
import game2dai.entityshapes.ps.PersonPic;
import game2dai.entityshapes.ps.Umbrella;
import game2dai.maths.Vector2D;
import game2dai.steering.AutoPilot;
import game2dai.steering.SBF;
import game2dai.utils.StopWatch;
import processing.core.PApplet;


/**
 * Test for 'can see' functionality
 * @author Peter
 *
 */
public class GameTest_013 extends PApplet{

	World world;
	Domain wd;

	Building[] buildings;
	Obstacle[] stalls;
	Vehicle[] tourists;
	Vehicle seeker;
	
	AutoPilot sbSeeker;
	
	int[] stallCol1, stallCol2;

	StopWatch watch, sw;

	Vector2D worldMouse = new Vector2D();
	Vector2D target = new Vector2D();
	
	int partSize = 100, partOverlap = 40;
	
	public void setup(){
		size(800,600);
		cursor(CROSS);
		stallCol1 = new int[] { color(174,44,79), color(255,227,40) };
		stallCol2 = new int[] { color(1,8,99), color(251,190,37), color(228,4,4), color(14,143,86) };

		world = new World(width, height, partSize, partOverlap);
//		world = new World(width, height);

		world.noOverlap(true);
		
//		System.out.println(world.getViewOnWorld());
//		world.resizeWorld(0.7f, World.VP_CENTER);
//		System.out.println(world.getViewOnWorld());
//		world.panWorldX(40);
//		world.panPixelY(23);
//		System.out.println(world.getViewOnWorld());

		wd = new Domain(0, 0, width, height);

		// Create the buildings
		buildings = Building.makeFromXML(this, "gt013_building.xml");
		for(Building b : buildings){
			BuildingPic bpic = new BuildingPic(this, color(200,96,96), color(0), 4.0f);
			b.renderer(bpic);
			world.add(b);
		}
		// Create the market stalls (round bits)
		stalls = Obstacle.makeFromXML(this, "gt013_obstacle.xml");
		for(Obstacle stall : stalls){
			Umbrella opic = new Umbrella(this, stall.colRadius(), stallCol2);
//			System.out.println(stall.getColRadius());
			stall.renderer(opic);
			world.add(stall);
		}
		// Create all the moving objects - start with the patrol
		AutoPilot sb = new AutoPilot();	// steering behaviour used as base for others
//		sb.setWallAvoidDetails(3, 2.8, 20);
//		sb.enableBehaviours(SBF.WALL_AVOID | SBF.OBSTACLE_AVOID);
//		// Create the patrol
//		// Now create the tourist
//		sb.setWanderDetails(1000, 10, 70);
//		sb.setWeight(SBF.SEPARATION, 30);
//		SB touristSB = (SB) sb.clone();
//		touristSB.enableBehaviours(SBF.WANDER | SBF.SEPARATION);
//		touristSB.setWanderDetails(100, 10, 70);
//		touristSB.setWallAvoidDetails(3, 2.8, 20);
		tourists = Vehicle.makeFromXML(this, "gt013_vehicle.xml");
		for(Vehicle tourist : tourists){
			PersonPic apic = new PersonPic(this, 10, color(255,160,255), color(145,64,47), color(0), 1);
			tourist.AP((AutoPilot) sb.clone());
			tourist.renderer(apic);
			tourist.worldDomain(wd);
			world.add(tourist);
		}

		seeker = new Vehicle(new Vector2D(300,300), // position
				6, // collision radius
				new Vector2D(10,10), // velocity
				40, // maximum speed
				new Vector2D(1,1), // heading
				1, // mass
				1,  // turning rate
				200 // max force
				); 
		PersonPic apic = new PersonPic(this, 10, color(160,160,255), color(160,144,70), color(0), 1);
		apic.showHints(Hints.HINT_VIEW | Hints.HINT_HEADING);
		seeker.renderer(apic);
		seeker.viewDistance(160);
		seeker.worldDomain(wd);
		sbSeeker = new AutoPilot();
//		sbSeeker.enableBehaviours(SBF.SEEK | SBF.WALL_AVOID | SBF.OBSTACLE_AVOID);
//		sbSeeker.setTarget(SBF.SEEK, target);
//		sbSeeker.enableBehaviours(SBF.SEEK);
		sbSeeker.seekOn(target).wallAvoidOn().obstacleAvoidOn();
//		sbSeeker.setWanderDetails(500, 30, 60);
		seeker.AP(sbSeeker);
		world.add(seeker);

		watch = new StopWatch(); // last thing to be done in setup
		sw = new StopWatch();
	}

	public void draw(){
		float deltaTime = (float) watch.getElapsedTime();
		found();
		target.set(mouseX, mouseY);
//		sbSeeker.setTarget(SBF.SEEK, target);
		sbSeeker.seekOn(target);

		background(220);
		pushMatrix();
		world.update(deltaTime);
		world.draw();
		
		
		popMatrix();
	}
	
	void found(){
		for(Vehicle tourist : tourists){
			if(seeker.canSee(world, tourist.pos().x, tourist.pos().y))
				((PersonPic)tourist.renderer()).bodyFill(color(255,255,0));
			else
				((PersonPic)tourist.renderer()).bodyFill(color(160,160,255));
		}
	}
}
